home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / s342q08.lha / libtabl.c < prev    next >
C/C++ Source or Header  |  1995-02-28  |  8KB  |  319 lines

  1. /*
  2. *                               libtabl.c
  3. *
  4. * Code to handle CTDLTABL.SYS
  5. */
  6. /*
  7. *                               history
  8. *
  9. * 87Jan20 HAW  Integrity stuff for portability.
  10. * 86Apr24 HAW  Modified for fwrite() and fread().
  11. * 85Nov15 HAW  Created.
  12. */
  13. #include "ctdl.h"
  14. /*
  15. *                               Contents
  16. *
  17. *       readSysTab()            restores system state from ctdltabl.sys
  18. *       common_read()           bottleneck for reading
  19. *       writeSysTab()           saves state of system in CTDLTABL.SYS
  20. *       GetDynamic()            allocation bottleneck
  21. */
  22. CONFIG          cfg;                    /* A buncha variables   */
  23. LogTable        *logTab;                /* RAM index of pippuls */
  24. NetTable        *netTab;                /* RAM index of nodes   */
  25. rTable          *roomTab;               /* RAM index of rooms   */
  26. EVENT           *EventTab = NULL;
  27. char            *indexTable = "ctdlTabl.sys";
  28. struct floor    *FloorTab;
  29. int             TopFloor;
  30. static char     *msg1 = "?old ctdlTabl.sys!";
  31. static struct
  32.   {
  33.   int checkMark;                        /* rudimentary integrity */
  34.   int cfgSize;                  /* sizeof cfg   */
  35.   int logTSize;                 /* logtab size  */
  36.   int endMark;                  /* another integrity check      */
  37.  
  38.   }
  39. integrity;
  40. extern char *R_W_ANY;
  41. static void *FindServes(char *name, char *target);
  42. SListBase Serves =
  43.   {
  44.   NULL, FindServes, NULL, free, NULL
  45.  
  46.   };
  47. /* These two should change from major release to */
  48. #define CHKM    7       /* major release        */
  49. #define ENDM    8
  50. /*
  51. * readSysTab()
  52. *
  53. * This function restores the state of system from CTDLTABL.SYS
  54. * returns:              TRUE on success, else FALSE
  55. * destroys CTDLTABL.TAB after read, to prevent erroneous re-use
  56. * in the event of a crash.
  57. *
  58. * MS-DOS fun: Here's the map --
  59. * Word 1 == sizeof cfg
  60. * Word 2 == sizeof logTab
  61. * Word 3 == sizeof roomTab
  62. * Word 4 -- thru x == cfg contents
  63. * x -- y == logTab
  64. * y -- z == roomTab
  65. * z -- a == netTab
  66. * EOF
  67. */
  68. char readSysTab(char kill, char showMsg)
  69.   {
  70.   FILE  *fd;
  71.   extern char *READ_ANY;
  72.   int           rover;
  73.   long  bytes;
  74.   SYS_FILE    name;
  75.   char  caller;
  76.   label       temp;
  77.   caller = cfg.weAre;
  78.   if ((fd = fopen(indexTable, READ_ANY)) == NULL)
  79.     {
  80.     if (showMsg) printf("?no %s!", indexTable);    /* Tsk, tsk! */
  81.     return(FALSE);
  82.  
  83.     }
  84.   if (fread(&integrity, sizeof integrity, 1, fd) != 1)
  85.     {
  86.     if (showMsg) printf(msg1);
  87.     return FALSE;
  88.  
  89.     }
  90.   if (     integrity.checkMark != CHKM ||
  91.   integrity.endMark != ENDM ||
  92.   integrity.cfgSize != sizeof cfg)
  93.     {
  94.     if (showMsg) printf(msg1);
  95.     return(FALSE);
  96.  
  97.     }
  98.   if (!common_read(&cfg, (sizeof cfg), 1, fd, showMsg))
  99.   return FALSE;
  100.   /* Allocations for dynamic parameters */
  101.   logTab = (LogTable *) GetDynamic(integrity.logTSize);
  102.   roomTab = (rTable *) GetDynamic(MAXROOMS * (sizeof (*roomTab)));
  103.   if (cfg.netSize)
  104.   netTab = (NetTable *) GetDynamic(sizeof (*netTab) * cfg.netSize);
  105.   else
  106.   netTab = NULL;
  107.   if (cfg.EvNumber)
  108.   EventTab  = (EVENT *)
  109.   GetDynamic(sizeof (*EventTab) * cfg.EvNumber);
  110.   /* "- 1" is kludge */
  111.   if (integrity.logTSize != sizeof (*logTab) * cfg.MAXLOGTAB)
  112.     {
  113.     if (showMsg) printf(msg1);
  114.     return(FALSE);
  115.  
  116.     }
  117.   if (!common_read(logTab, integrity.logTSize, 1, fd, showMsg))
  118.   return FALSE;
  119.   if (!common_read(roomTab, (sizeof (*roomTab)) * MAXROOMS, 1, fd, showMsg))
  120.   return FALSE;
  121.   if (cfg.netSize)
  122.     {
  123.     for (rover = 0; rover < cfg.netSize; rover++)
  124.       {
  125.       if (!common_read(&netTab[rover], NT_SIZE, 1, fd, showMsg))
  126.       return FALSE;
  127.       netTab[rover].netTRooms =
  128.       (SharedRoom *) GetDynamic(SR_BULK);
  129.       if (!common_read(netTab[rover].netTRooms, SR_BULK, 1, fd, showMsg))
  130.       return FALSE;
  131.  
  132.       }
  133.  
  134.     }
  135.   if (cfg.EvNumber)
  136.     {
  137.     if (!common_read(EventTab, (sizeof(*EventTab) * cfg.EvNumber), 1, fd,
  138.     showMsg))
  139.     return FALSE;
  140.  
  141.     }
  142.   for (rover = 0; rover < cfg.DomainHandlers; rover++)
  143.     {
  144.     if (!common_read(temp, NAMESIZE, 1, fd, showMsg))
  145.       {
  146.       return FALSE;
  147.  
  148.       }
  149.     AddData(&Serves, strdup(temp), NULL, FALSE);
  150.  
  151.     }
  152.   fclose(fd);
  153.   makeSysName(name, "ctdlflr.sys", &cfg.floorArea);
  154.   if ((fd = fopen(name, R_W_ANY)) == NULL)
  155.     {
  156.     if (caller != CONFIGUR)
  157.       {
  158.       if (showMsg) printf("No floor table!");
  159.       return FALSE;
  160.  
  161.       }
  162.  
  163.     }
  164.   else
  165.     {
  166.     totalBytes(&bytes, fd);
  167.     FloorTab = (struct floor *) GetDynamic((int) bytes);
  168.     if (fread(FloorTab, (int) bytes, 1, fd) != 1)
  169.       {
  170.       if (showMsg) printf("problem reading floor tab");
  171.       fclose(fd);
  172.       if (caller != CONFIGUR) return FALSE;
  173.  
  174.       }
  175.     else
  176.       {
  177.       fclose(fd);
  178.       TopFloor = (int) bytes/sizeof(*FloorTab);
  179.  
  180.       }
  181.  
  182.     }
  183.   if (kill) unlink(indexTable);
  184.   crypte(cfg.sysPassword, sizeof cfg.sysPassword, 0);
  185.   return(TRUE);
  186.  
  187.   }
  188. /*
  189. * common_read()
  190. *
  191. * This function reads in from file the important stuff.
  192. * returns:      TRUE on success, else FALSE
  193. */
  194. int common_read(void *block, int size, int elements, FILE *fd,
  195. char showMsg)
  196.   {
  197.   if (size == 0) return TRUE;
  198.   if (fread(block, size, elements, fd) != 1)
  199.     {
  200.     if (showMsg) printf(msg1);
  201.     return FALSE;
  202.  
  203.     }
  204.   return TRUE;
  205.  
  206.   }
  207. static FILE *fd;
  208. /*
  209. * writeSysTab()
  210. *
  211. * This saves state of system in CTDLTABL.SYS
  212. * returns:      TRUE on success, else ERROR
  213. * See readSysTab() to see what the CTDLTABL.SYS map looks like.
  214. */
  215. static void WriteServers(char *name);
  216. int writeSysTab()
  217.   {
  218.   extern char   *WRITE_ANY;
  219.   int   rover;
  220.   if ((fd = fopen(indexTable, WRITE_ANY)) == NULL)
  221.     {
  222.     printf("?can't make %s", indexTable);
  223.     return(ERROR);
  224.  
  225.     }
  226.   /* Write out some key stuff so we can detect bizarreness: */
  227.   integrity.checkMark = CHKM;
  228.   integrity.endMark = ENDM;
  229.   integrity.cfgSize = sizeof cfg;
  230.   integrity.logTSize = sizeof (*logTab) * cfg.MAXLOGTAB;
  231.   fwrite(&integrity, (sizeof integrity), 1, fd);
  232.   crypte(cfg.sysPassword, sizeof cfg.sysPassword, 0);
  233.   fwrite(&cfg, (sizeof cfg), 1, fd);
  234.   crypte(cfg.sysPassword, sizeof cfg.sysPassword, 0);
  235.   fwrite(logTab, (sizeof(*logTab) * cfg.MAXLOGTAB), 1, fd);
  236.   fwrite(roomTab, (sizeof (*roomTab)) * MAXROOMS, 1, fd);
  237.   for (rover = 0; rover < cfg.netSize; rover++)
  238.     {
  239.     fwrite(&netTab[rover], NT_SIZE, 1, fd);
  240.     fwrite(netTab[rover].netTRooms, SR_BULK, 1, fd);
  241.  
  242.     }
  243.   if (cfg.EvNumber)
  244.   fwrite(EventTab, (sizeof(*EventTab) * cfg.EvNumber), 1, fd);
  245.   RunList(&Serves, WriteServers);
  246.   fclose(fd);
  247.   return(TRUE);
  248.  
  249.   }
  250. /*
  251. * WriteServers()
  252. *
  253. * This function writes a domain server out to ctdltabl.sys.  See DOMAINS.C
  254. * for more information on this list.
  255. */
  256. static void WriteServers(char *name)
  257.   {
  258.   fwrite(name, NAMESIZE, 1, fd);
  259.  
  260.   }
  261. /*
  262. * GetDynamic()
  263. *
  264. * This does mallocs with error checking.
  265. */
  266. void *special_GetDynamic(unsigned size, char *file, int line)
  267.   {
  268.   void *temp;
  269.   char msg[80];
  270. /**
  271.   if (cfg.BoolFlags.debug)
  272.     {
  273.     splitF(NULL,"GetDynamic(%04.4x, %s, %d)\n",size,file,line);
  274.     };
  275. **/
  276.   if (size == 0) return NULL; /* Simplify code  */
  277.   temp = calloc(1,size);
  278.   /* printf("Requested %d bytes, received address %p\n", size, temp); */
  279.   if (temp == NULL)
  280.     {
  281.     printf("Request for %u bytes of memory failed.\n", size);
  282.     sprintf(msg, "Asked for %u bytes, unable to get it.\n", size);
  283.     crashout(msg);
  284.     }
  285.   return temp;
  286.  
  287.   }
  288. /*
  289. * openFile()
  290. *
  291. * This opens one of the .sys files.
  292. */
  293. void openFile(char *filename, FILE **fd)
  294.   {
  295.   /* We use fopen here rather than safeopen for link reasons */
  296.   if ((*fd = fopen(filename, R_W_ANY)) == NULL)
  297.     {
  298.     printf("?no %s", filename);
  299.     exit(SYSOP_EXIT);
  300.  
  301.     }
  302.  
  303.   }
  304. /*
  305. * FindServes()
  306. *
  307. * This is a find the server function.  It's used in the list of domain servers
  308. * to allow us to search for a domain server based on the name of the domain.
  309. */
  310. static void *FindServes(char *name, char *target)
  311.   {
  312.   if (cfg.BoolFlags.debug)
  313.     {
  314.     splitF(NULL,"FindServes( %s, %s)\n",name, target);
  315.     };
  316.   return (strCmpU(name, target) == SAMESTRING) ? name : NULL;
  317.  
  318.   }
  319.